/**
 * @author Patrick
 * This is the sub class for CompositeType, ArrayType. It contains two private fields and they are 
 * array_size and basicType.
 */
public class ArrayType extends CompositeType{
	
	private int numOfElements ; // size of array 
	private Type baseType ; // basic type for array
	
	
	/*
	 * Default ctor that creates an ArrayType object with
	 * name "array" and size will be determine of the basicType (bytes).
	 */
	public ArrayType() {
		super("array", 4);
	}
	
	public ArrayType(String strName) {
		super(strName, 4);
	}
	
	public int getNumOfElements() {
		return numOfElements;
	}
	public void setNumOfElements(int numOfElements) {
		this.numOfElements = numOfElements;
	}
	public Type getBaseType() {
		return baseType;
	}
	public void setBaseType(Type baseType) {
		this.baseType = baseType;
	}
	
	@Override
	public int getSize() {
		int size = getNumOfElements() * (getBaseType().getSize());
		return size;
	}
	@Override
	public boolean isArrayType() { 
		return true; 
	}
	public boolean isAssignableTo(Type t) {
		if(t.isArrayType()) {
			ArrayType tParam = (ArrayType) t ;
			if (this.getBaseType().isAssignableTo(tParam.getBaseType())) {
				if(this.getNumOfElements() == tParam.getNumOfElements()) {
					return true;
				} 
			} 
		}
		if(t.isPointerType()) {
			if(this.getBaseType().isEquivalentTo(((PointerType)t).getBaseType())) {
				return true;
			}
		}
		return false;
	}
	public boolean isEquivalentTo(Type t) { 
		if(t.isArrayType()) {
			ArrayType tParam = (ArrayType) t ;
			if (this.getBaseType().isEquivalentTo(tParam.getBaseType())) { // the order doesnt matter since Equivalence
				if(this.getNumOfElements() == tParam.getNumOfElements()) {
					return true;
				}
			}
		}
		return false;
	}
}
